/*
 * @Author: steven.wang
 * @Date:   2019-05-13 14:58:44
 * @Last Modified by:   steven.wang
 * @Last Modified time: 2019-05-15 10:48:25
 */
const moment = require('moment');
const path = require('path');
const OSS = require('ali-oss');
const PhotoModel = require('../models/crawler/photo');
const jsonSendFormat = require('../utils/tools').jsonSendFormat;
const client = new OSS({
    region: 'oss-cn-shanghai',
    accessKeyId: 'EKyZ4QasNKjKXgL5',
    accessKeySecret: 'l7CGG5VTvsOjRjNTaHDN5tM1Q10L41',
    bucket: 'm-steven'
});

exports.upload = async (req, res) => {
    try {
      	let now = moment();
    	let filePath = 'img/' + (req.body.type ? req.body.type + '/' : '') +
    		now.format('YYYY') + '/' + now.format('MM') +
    		'/' + now.format('DD') + '/' + req.file.filename;
        let result = await client.put(filePath, req.file.path);
        res.json(jsonSendFormat(200, {
            filename: req.file.filename,
            mimetype: req.file.mimetype,
            size: req.file.size,
            url: result.url
        }))
    } catch (err) {
        res.json(jsonSendFormat(500, err))
    }
}